<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<LINK rel="Bookmark" href="/favicon.ico" >
<LINK rel="Shortcut Icon" href="/favicon.ico" />
<!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<script type="text/javascript" src="lib/PIE_IE678.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="../static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="../lib/Hui-iconfont/1.0.7/iconfont.css" />
<link rel="stylesheet" type="text/css" href="../lib/icheck/icheck.css" />
<link rel="stylesheet" type="text/css" href="../static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/style.css" />
<!--[if IE 6]>
<script type="text/javascript" src="http://lib.h-ui.net/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<!--/meta 作为公共模版分离出去-->

<title>猪买卖添加</title>
</head>
<body>
<article class="page-container">
<div class="cl pd-5 bg-1 bk-gray mt-20"> <span class="l"><a class="btn btn-primary radius" onclick="add()" href="javascript:;"><i class="Hui-iconfont">&#xe600;</i> 添加一行</a></span> </div>
	<div class="mt-20">
		<table id="tableList" class="table table-border table-bordered table-bg table-sort">
			<thead>
				<tr class="text-c">
					<th width="90">耳号</th>
					<th width="90">时间</th>
					<th width="65">单价</th>
					<th width="65">重量</th>
					<th width="70">小计</th>
					<th width="70">交易类型</th>
					<th width="40">操作</th>
				</tr>
				<tr id="clone" class="text-c trTotal" style="display: none;">
					<td>
						<span class="select-box l">
							<select class="select" size="1" name="deal_ear">
								<option value="">选择耳号</option>
							</select>
						</span>
					<td><input type="text" class="input-text Wdate" onfocus="WdatePicker({isShowClear:true,readOnly:true})" placeholder="时间" name="deal_time"></td>
					<td><input type="text" class="input-text" value="" placeholder="单价" name="deal_price"></td>
					<td><input type="text" class="input-text" value="" placeholder="重量" name="deal_weight"></td>
					<td><span class="pig_total"></span></td>
					<td>
						<span class="select-box l">
							<select class="select" size="1" name="deal_buy_sell">
								<option value="卖出">卖出</option>
								<option value="购入">购入</option>
							</select>
						</span>
					</td>
					<td class="c"><button type="button" onclick="deltr(this);" class="btn btn-default radius text-c">删除</button></td>
				</tr>
			</thead>
			<tbody>
				<tr class="text-c trTotal">
					<td>
						<span class="select-box l">
							<select class="select" size="1" name="deal_ear">
								<option value="">选择耳号</option>
							</select>
						</span>
					<td><input type="text" class="input-text Wdate" onfocus="WdatePicker({isShowClear:true,readOnly:true})" placeholder="时间" name="deal_time"></td>
					<td><input type="text" class="input-text" value="" placeholder="单价" name="deal_price"></td>
					<td><input type="text" class="input-text" value="" placeholder="重量" name="deal_weight"></td>
					<td><span class="pig_total"></span></td>
					<td>
						<span class="select-box l">
							<select class="select" size="1" name="deal_buy_sell">
								<option value="卖出">卖出</option>
								<option value="购入">购入</option>
							</select>
						</span>
					</td>
					<td class="c"><button type="button" onclick="deltr(this);" class="btn btn-default radius text-c">删除</button></td>
				</tr>
			</tbody>
		</table>

		<form id="orderForm" class="form form-horizontal">
			<input type="text" id="order_sub" name="order_sub" hidden="hidden"/>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>订单日期：</label>
				<div class="formControls col-xs-8 col-sm-10">
					<input type="text" onfocus="WdatePicker({maxDate:'%y-%M-%d'})" id="order_date" name="order_date" class="input-text Wdate" >
				</div>
			</div>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>合作商：</label>
				<div class="formControls col-xs-8 col-sm-10">
					<span class="select-box">
						<select class="select select-option" size="1"  id="order_sup" name="order_sup">
							<option value="">选择合作商</option>
						</select>
					</span>
				</div>
			</div>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>采购人：</label>
				<div class="formControls col-xs-8 col-sm-10">
					<span class="select-box">
						<select class="select select-option" size="1"  id="order_emp" name="order_emp" >
							<option value="">选择采购人员</option>
						</select>
					</span>
				</div>
			</div>

			<div class="row cl">
				<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
					<button type="submit" class="btn btn-success radius"><i class="icon-ok"></i> 提交</button>
				</div>
			</div>
		</form>
	</div>
</article>


<script type="text/javascript" src="../lib/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="../lib/layer/2.1/layer.js"></script> 
<script type="text/javascript" src="../lib/icheck/jquery.icheck.min.js"></script> 
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/jquery.validate.min.js"></script> 
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/messages_zh.min.js"></script> 
<script type="text/javascript" src="../static/h-ui/js/H-ui.js"></script> 
<script type="text/javascript" src="../static/h-ui.admin/js/H-ui.admin.js"></script> 
<script type="text/javascript" src="../lib/My97DatePicker/WdatePicker.js"></script> 
<script type="text/javascript" src="../lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../MyJs/jqueryExtend.js"></script>

<script type="text/javascript">
	//添加一行
	function add(){
		var $tr = $("#clone").clone();
		$($tr).removeAttr("style");
		$(".table>tbody").append($tr);
	}
	//删除一行
	function deltr(obj){
		$(obj).parents("tr").remove();
	}

    $(document).on('input propertychange', '[name="deal_price"]' , function() { //监听 deal_price 值改变，计算小计
        var price = parseFloat($(this).val()?$(this).val():0);
        var weight = parseFloat($(this).parents('.trTotal').find('[name="deal_weight"]').val()?$(this).parents('.trTotal').find('[name="deal_weight"]').val():0);
        var total = price * weight;
        $(this).parents('.trTotal').find('.pig_total').text(total);
    });

    $(document).on('input propertychange','[name="deal_weight"]', function() { //监听 deal_weight 值改变，计算小计
        var price = parseFloat($(this).parents('.trTotal').find('[name="deal_price"]').val()?$(this).parents('.trTotal').find('[name="deal_price"]').val():0);
        var weight = parseFloat($(this).val()?$(this).val():0);
        var total = price * weight;
        $(this).parents('.trTotal').find('.pig_total').text(total);
    });

	$("#pig_price").bind('input propertychange', function() { //监听pur_price值改变，计算小计
		var pur_price=$("#pig_price").val();
		var pur_count=$("#pig_count").val();
		$("#pig_total").text(pur_price*pur_count);
	});
	$("#pig_count").bind('input propertychange', function() {//监听pur_count值改变，计算小计
		var pur_price=$("#pig_price").val();
		var pur_count=$("#pig_count").val();
		$("#pig_total").text(pur_price*pur_count);
	});
</script>

<script type="text/javascript">
    $(function(){
        $(window).load(function(){
            init();
        });

        $("#orderForm").validate({
            rules:{
                order_date:{
                    required:true,
                },
                order_sup:{
                    required:true,
                },
                order_emp:{
                    required:true,
                },
            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var orderData = $(form).serializeObject();
                var dealPigList = new Array();
                $.each($("#tableList tbody tr"),function(i,v){
                    var obj = new Object();
                    obj.deal_ear = $(this).find('td > span > select[name="deal_ear"]').val();
                    obj.deal_time = $(this).find('td > input[name="deal_time"]').val();
                    obj.deal_price = $(this).find('td > input[name="deal_price"]').val();
                    obj.deal_weight = $(this).find('td > input[name="deal_weight"]').val();
                    obj.deal_buy_sell = $(this).find('td > span > select[name="deal_buy_sell"]').val();
                    dealPigList[i] = obj;
                });
                var submit = new Object();
                submit.orders = orderData;
                submit.dealPigList = dealPigList;
                console.log(submit);
                $.ajax({
                    type:"POST",
                    url:"/yewumanage/deal_pig/insertDealPigInfo.do",
                    dataType:"json",
                    data:JSON.stringify(submit),
                    contentType:"application/json",
                    success:function (data) {
                        if(data === true){
                            layer.alert("添加成功！",function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.$('.btn-refresh').click();
                                parent.layer.close(index);
                            })
                        }else {
                            layer.msg('添加失败!',{icon:1,time:1000});
                        }
                    },
                    error:function () {
                        layer.msg('添加失败!',{icon:1,time:1000});
                    }
                });
            }
        });
    });

    //初始化方法
    function init(){

        //加载耳号
        $.post("/pig_manage/pig/getPigList.do", function(data) {
            $(data).each(function(i,v) {
                var content = "<option value="+v.pig_id+">"+v.pig_ear_num+"</option>";
                $('[name="deal_ear"]').append(content);
            });
        },"json");

        //加载人员
        $.post("/staffing/employee/getEmployeeList.do",function(data){
            $.each(data,function(i,v){
                var $optionPeo = "<option value="+v.emp_id+">"+v.emp_name+"</option>";
                $("#order_emp").append($optionPeo);
            });
        },"json");

        //加载合作商
        $.post("/yewumanage/partner/getPartnerList.do",function(data){
            $.each(data,function(i,v){
                var $option = "<option value="+v.par_id+">"+v.par_name+"</option>";
                $("#order_sup").append($option);
            });
        },"json");
    }
</script>
</body>
</html>